package com.woniuxy.controller;

import com.github.pagehelper.PageInfo;
import com.woniuxy.annotation.MyLog;
import com.woniuxy.annotation.RequirePermission;
import com.woniuxy.entity.Dept;
import com.woniuxy.entity.Hospital;
import com.woniuxy.service.hospital.DeptService;
import com.woniuxy.util.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
public class DeptController {
    @Autowired
    private DeptService deptService;

    @RequirePermission("dept:find")
    @RequestMapping("dept/findAll")
    public ResultVo findAll(@RequestBody Map<String,String> map){
        String strPage =map.remove("page");
        String strSize =map.remove("size");
        PageInfo<Dept> list = deptService.findAll(Integer.valueOf(strPage),Integer.valueOf(strSize));
        return ResultVo.success(list);
    }

    @MyLog("修改科室信息")
    @RequirePermission("dept:update")
    @PostMapping("dept/update")
    public ResultVo update(@RequestBody Dept dept) {
        int rows = deptService.update(dept);
        return rows > 0 ? ResultVo.success() : ResultVo.failure();
    }


    @MyLog("新增科室")
    @RequirePermission("dept:save")
    @PostMapping("dept/save")
    public ResultVo save(@RequestBody Dept dept) {
        int rows = deptService.save(dept);
        return rows > 0 ? ResultVo.success() : ResultVo.failure();
    }
    @MyLog("删除科室")
    @RequirePermission("dept:delete")
    @PostMapping("dept/delete")
    public ResultVo delete(@RequestBody String deptId) {
        if(!StringUtils.hasText(deptId)){
            throw new RuntimeException("deptId is required");
        }
        int rows = deptService.delete(Integer.valueOf(deptId));
        return rows > 0 ? ResultVo.success() : ResultVo.failure();
    }


    @PostMapping("dept/findLike")
    public ResultVo findLike(@RequestBody String hName) {
        List<Dept> list = deptService.findLike(hName);
        return  ResultVo.success(list);
    }


    @PostMapping("dept/find")
    public ResultVo find(@RequestBody Hospital hospital){
        Integer hId = hospital.gethId();
        List<Dept> allDept = deptService.findAllDept(hId);
        return ResultVo.success(allDept);
    }
}
